package com.market.service.admin.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.market.bean.MarketIssue;
import com.market.bean.MarketIssueExample;
import com.market.bean.common.BasePageInfo;
import com.market.bean.common.CommonData;
import com.market.mapper.MarketIssueMapper;
import com.market.service.admin.AdminIssueService;
import com.market.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author fan
 * @Description:
 * @since 2022/12/30 22:12
 */
@Service
public class AdminIssueServiceImpl implements AdminIssueService {


    @Autowired
    MarketIssueMapper issueMapper;

    @Override
    public CommonData<MarketIssue> list(BasePageInfo info, String question ) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketIssueExample example = new MarketIssueExample();
        example.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketIssueExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(question)) {
            criteria.andQuestionLike("%" + question + "%");
        }
        criteria.andDeletedEqualTo(false);
        List<MarketIssue> list = issueMapper.selectByExample(example);
        PageInfo<MarketIssue> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    @Override
    public MarketIssue create(MarketIssue marketIssue) {
        Date date = new Date();
        marketIssue.setAddTime(date);
        marketIssue.setUpdateTime(date);
        issueMapper.insertSelective(marketIssue);
        return marketIssue;
    }

    @Override
    public MarketIssue update(MarketIssue marketIssue) {
        Date date = new Date();
        marketIssue.setUpdateTime(date);
        issueMapper.updateByPrimaryKeySelective(marketIssue);
        return marketIssue;
    }

    @Override
    public void delete(MarketIssue marketIssue) {
        marketIssue.setDeleted(true);
        issueMapper.updateByPrimaryKeySelective(marketIssue);
    }
}
